﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using NetCommand;

[Serializable]
[ComVisible(true)]
public struct Int24
{
    public byte[] Data;
      
    public const int MaxValue = 2147483647;
   
    public const int MinValue = -2147483648;
    public static bool operator ==(Int24 s, int n)
    {
        byte[] data = EncodingTools.intToBytes(n);
        return data != s.Data;
    }

    public static bool operator !=(Int24 s, int n)
    {
        byte[] data = EncodingTools.intToBytes(n);
        return s.Data != data;
    }
     
    public byte this[int index]
    {
        get
        {
            if (Data == null)
            {
                Data = new byte[3] { 0, 0, 0};
            }
            return Data[index];
        }
        set
        {
            if (Data == null)
            {
                Data = new byte[3];
            }
            Data[index] = value;
        }
    }

    public void SetValue(byte[] value)
    {
        if (value.Length != 3)
        {
            return;
        }
        Data[0] = value[0];
        Data[1] = value[1];
        Data[2] = value[2];
    }

    public void SetValue(byte _1, byte _2, byte _3)
    {
        if (Data == null)
        {
            Data = new byte[3] { 0, 0, 0 };
        }
        Data[0] = _1;
        Data[1] = _2;
        Data[2] = _3;
    }

    public void SetValue(int value)
    {
        byte[] data = EncodingTools.intToBytes(value);
        Data = data;
    }

    public static bool operator ==(Int24 s, Int24 n)
    {
        return n.Data == s.Data;
    }

    public override bool Equals(object obj)
    {
        return base.Equals(obj);
    }

    Int24(int value)
    {
        Data = new byte[3];
        SetValue(value);
    }

    public static bool operator !=(Int24 s, Int24 n)
    {
        return s.Data != n.Data;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

    public static bool operator >(Int24 s, Int24 n)
    {
        int sV = convertDataToInt(s.Data);
        int nV = convertDataToInt(n.Data);
        return sV > nV;
    }

    public static bool operator <(Int24 s, Int24 n)
    {
        int sV = convertDataToInt(s.Data);
        int nV = convertDataToInt(n.Data);
        return sV < nV;
    }

    public override string ToString()
    {
        return ToInt().ToString();
    }

    public int ToInt()
    {
        return convertDataToInt(Data);
    }

    public int GetSize()
    {
        return 3;
    }

    private static int convertDataToInt(byte[] data)
    {
        int value;
        value = (int)(((data[0] & 0xFF) << 16)
                | ((data[1] & 0xFF) << 8)
                | (data[2] & 0xFF));
        return value;
    }
        
}

